<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户管理
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-02-25 13:43:37 +0800 (二, 2014-02-25) $
 * @version : $Id: admin.php 31 2014-02-25 05:43:37Z spike $
 * @rev     : $Revision: 31 $
 */
class Admin extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Rbac_model');
            $list = $this->Rbac_model->browse_admin($params, $params['page'], $params['size']);

            $this->Rbac_model->wrap_role_info($list['data'], FALSE);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 统计
        $this->load->dao('Admin_dao');
        $total = $this->Admin_dao->count(array(
            'is_delete' => UNDELETED
        ));

        // 筛选
        $admin_gender = $this->config->item('admin_gender');
        $admin_status = $this->config->item('admin_status');
        $pagesize     = $this->config->item('pagesize');

        $this->load->dao('Role_dao');
        $admin_role   = $this->Role_dao->get(
            array('is_delete' => UNDELETED, 'status' => ROLE_STATUS_ON),
            array('role_id','name')
        );

        $data = array(
            'default'     => $params ? $params : array(),
            'total'       => number_format($total),
            'admin_gender'=> $admin_gender,
            'admin_status'=> $admin_status,
            'admin_role'  => $admin_role,
            'list'        => $list,
            'pagination'  => $pagination,
            'pagesize'    => $pagesize,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('admin/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [查看单个用户]
     */
    public function view($id = NULL)
    {
        $this->load->model('Rbac_model');
        if( ! $admin = $this->Rbac_model->get_admin($id, FALSE, TRUE))
        {
            show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
        }

        // 绑定角色名称
        $this->Rbac_model->wrap_role_info($admin, TRUE);
        
        // 获取所有资源类型
        $role_resource_group = $this->config->item('role_resource_group');

        // 获取所有状态
        $admin_status = $this->config->item('admin_status');

        // 获取所有资源列表
        $this->load->dao('Role_resource_dao');
        $role_resource = $this->Role_resource_dao->get(
            array('is_delete' => UNDELETED),
            array('resource_id','name')
        );

        // 绑定操作人数据
        $this->load->model('Rbac_model');
        $this->Rbac_model->wrap_admin_info($admin, TRUE, 'create_admin');
        $this->Rbac_model->wrap_admin_info($admin, TRUE, 'update_admin');

        $page_style = $this->config->item('page_style');
        $data = array(
            'admin'               => $admin,
            'admin_gender'        => $this->config->item('admin_gender'),
            'admin_status'        => $admin_status,
            'role_resource'       => $role_resource,
            'role_resource_group' => $role_resource_group,
            'page_style'          => $page_style['LITE_GREY'],
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('admin/view.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增用户]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('admin_add', $params);

            $this->load->model('Rbac_model');
            if( FALSE === $admin_id = $this->Rbac_model->add_admin($params) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }
            temp('admin_add', NULL);

            show_message(
                "新增用户成功！", 
                array('查看新增用户' => DOMAIN."admin/view/{$admin_id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->dao('Role_dao');
            $role = $this->Role_dao->get(
                array(
                    'is_delete' => UNDELETED,
                    'status'    => ROLE_STATUS_ON,
                ),
                array('role_id','name')
            );

            $page_style = $this->config->item('page_style');
            $data = array(
                'temp'         => temp('admin_add'),
                'admin_gender' => $this->config->item('admin_gender'),
                'role'         => $role,
                'page_style'   => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('admin/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [改变用户状态]
     */
    public function change_status($id = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Rbac_model');
        if( FALSE === $this->Rbac_model->change_admin_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Rbac_model->get_error());
            }
            else
            {
                show_error($this->Rbac_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("改变用户状态成功！", array(
                    '返回用户管理' => DOMAIN."admin/browse",
                ));
            }
        }
    }

    /**
     * [编辑角色]
     */
    public function edit_role($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Rbac_model');

            if( FALSE === $this->Rbac_model->edit_admin_role($id, $params['roles']) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑用户角色成功！", 
                array('查看用户角色' => DOMAIN."admin/edit_role/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Rbac_model');
            if( FALSE === $admin = $this->Rbac_model->get_admin($id, TRUE, FALSE, TRUE) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }

            // 获取所有用户
            $this->load->dao('Role_dao');
            $roles = $this->Role_dao->get(
                array('is_delete' => UNDELETED, 'status' => ROLE_STATUS_ON),
                array('role_id','name')
            );

            $page_style = $this->config->item('page_style');
            $data = array(
                'admin'      => $admin,
                'roles'      => $roles,
                'page_style' => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('admin/edit_role.tpl');
            $this->load->view('home/footer.tpl');
        }
    }
    
    /**
     * [删除所有角色]
     */
    public function del_role($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $this->load->model('Rbac_model');
            
            if( FALSE === $this->Rbac_model->del_admin_role($id) )
            {
                show_error($this->Rbac_model->get_error(), 200, '错误', 'error_general_lite');
            }
            
            show_message(
                "删除用户角色成功！", 
                array('查看用户角色' => DOMAIN."admin/edit_role/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
    }
}

/* End of file Admin.php */